<template>
  <!-- <div :class='s.wrap' :style='{ height: (isLoading || views.length === 0)? screenH + "px" : ""}'> -->
  <div :class='s.wrap' :style='{ height: screenH + "px"}'>
    <!-- <action-bar
      title='观点PK'
      color='#000'
      :class='s.actionBar'>
      <template slot='left'>
        <action-button color='#ddd' @click='onBack'>
          <img src="~assets/images/return.png" :class='s.icon'>
        </action-button>
      </template>
      <template slot='right'>
        <action-button color='transparent'>
        </action-button>
      </template>
    </action-bar> -->
    <div :class='s.body'>
      <div v-if='isLoading' :class='s.isLoading'>加载中...</div>
      <div v-if='views.length === 0  && !isLoading'
         :class='s.isLoading'>啊咧咧>_<没有新观点</div>
      <template v-if='views.length > 0 && !isLoading'>
        <h1>{{views[0].VOTE_TITLE}}</h1>
        <div :class='s.img' v-if='views[0].VOTE_PIC.length > 0'>
          <img :src='"https://www.gtja.com/f/" + views[0].VOTE_PIC'>
        </div>
        <div :class='s.contentWrap'>
          <pre :class='s.pre'>{{views[0].SUMMARY}}</pre>
          <!-- <textarea :class='s.content' readonly="readonly">{{views[0].SUMMARY}}</textarea> -->
        </div>
        <div
          v-if='views[0].OPTION_LIST.length >= 2'
          :class='s.pk'
          :style='{ minHeight: (screenW / ratio) + "px"}'>
          <div :class='s.left' @click='onLeft'>
            <div>红方观点</div>
            <div :class='s.view'>{{views[0].OPTION_LIST[0].OPTION_TITLE}}</div>
          </div>
          <div :class='s.mid'>
            <div :class='s.leftP' :style='{width: redPrecent}'>
              <div :class='s.p'></div>
              <div :class='s.precent'>{{ views[0].OPTION_LIST[0].OPTION_RESULT }}</div>
            </div>
            <div :class='s.rightP' :style='{width: bluePrecent}'>
              <div :class='s.p'></div>
              <div :class='s.precent'>{{ views[0].OPTION_LIST[1].OPTION_RESULT }}</div>
            </div>
          </div>
          <div :class='s.right' @click='onRight'>
            <div>蓝方观点</div>
            <div :class='s.view'>{{views[0].OPTION_LIST[1].OPTION_TITLE}}</div>
          </div>
        </div>
        <div :class='s.history' v-if='views.length > 1'>
          <div :class='s.historyTitle'>往期回顾</div>
          <div :class='s.itemWrap' v-for='(view, index) in views' v-if='index !== 0'><div :class='s.point'></div><div :class='s.view'>{{view.VOTE_TITLE}}</div></div>
        </div>
        <img
          v-if='views[0].IS_COMMENT === "1"'
          src="~assets/images/pinglun.png"
          :class='s.plIcon'
          @click='onComment(views[0].VOTE_ID)'>
        <div :class='s.speakWrap' v-if='views[0].IS_COMMENT === "1"'>
          <speak :class='s.speak' :voteId='views[0].VOTE_ID' :voteTitle='views[0].VOTE_TITLE'></speak>
        </div>
      </template>
    </div>
  </div>
</template>

<script>
// import ActionBar from 'components/actionBar'
// import ActionButton from 'components/actionBar/ActionButton'
import Speak from 'components/comment/Speak'
import RemoteDAO from 'common/RemoteDAO'
import checkLogin from 'components/checkLogin'

export default {
  components: { Speak },
  data () {
    return {
      bgpk: require('assets/images/pkbg.png'),
      content: 'Content Content Content Content Content',
      leftView: 'view',
      rightView: 'view',
      ratio: 2.24,
      screenW: 0,
      screenH: 0,
      redPrecent: '50%',
      bluePrecent: '50%',
      isLoading: true,
      votes: [
        {
          VOTE_ID: '',
          VOTE_TITLE: '',
          VOTE_PIC: '',
          AUTHOR: '',
          IS_COMMENT: '',
          SUMMARY: '',
          VOTE_STATUS: 0, // 0-未发布 1-已发布 2-已下架
          BEGIN_DATE: '',
          END_DATE: '',
          OPTION_LIST: [
            {
              OPTION_ID: '',
              OPTION_TYPE: '1', // 选项类型  1：单选框   2：复选框   3：文本框，用户输入文本内容
              OPTION_TITLE: ''
            },
            {
              OPTION_ID: '',
              OPTION_TYPE: '1', // 选项类型  1：单选框   2：复选框   3：文本框，用户输入文本内容
              OPTION_TITLE: ''
            }
          ]
        }
      ],
      views: []
    }
  },
  watch: {
    votes: function (val) {
      var result = this.votes.filter(item => {
        return item.VOTE_STATUS === '1'
      })
      this.views = result
      console.info('views', this.views)
      if (result.length > 0) {
        this.fetchOptionResult(result[0].VOTE_ID).then(resp => {
          console.info('fetchOptionResult', resp)
          this.setOptionResult(resp.data.body)
        }).catch(error => {
          console.error('fetchOptionResult', error)
        })
      }
    }
  },
  mounted () {
    this.screenW = window.innerWidth
    this.screenH = window.innerHeight
    this.initUI()
    this.fetchView()
  },
  updated () {
    this.screenW = window.innerWidth
    this.screenH = window.innerHeight
  },
  methods: {
    computePrecent () {
      if (this.views[0].OPTION_LIST[0].OPTION_RESULT === this.views[0].OPTION_LIST[1].OPTION_RESULT) {
        this.redPrecent = '50%'
        this.bluePrecent = '50%'
      } else {
        this.redPrecent = (this.views[0].OPTION_LIST[0].OPTION_RESULT / (this.views[0].OPTION_LIST[0].OPTION_RESULT + this.views[0].OPTION_LIST[1].OPTION_RESULT)) * 100 + '%'
        console.info('this.redPrecent', this.redPrecent)
        this.bluePrecent = (this.views[0].OPTION_LIST[1].OPTION_RESULT / (this.views[0].OPTION_LIST[0].OPTION_RESULT + this.views[0].OPTION_LIST[1].OPTION_RESULT)) * 100 + '%'
      }
    },
    onLeft () {
      this.postVote(this.views[0].VOTE_ID, this.views[0].OPTION_LIST[0].OPTION_ID, this.views[0].OPTION_LIST[0].OPTION_TITLE).then(resp => {
        this.fetchOptionResult(this.views[0].VOTE_ID).then(resp => {
          console.info('onLeft', resp)
          if (resp.error === '0') {
            this.setOptionResult(resp.data.body)
            this.computePrecent()
          }
        })
      }).catch(error => {
        this.$store.dispatch('setTip', {text: error.message})
      })
    },
    onRight () {
      this.postVote(this.views[0].VOTE_ID, this.views[0].OPTION_LIST[1].OPTION_ID, this.views[0].OPTION_LIST[1].OPTION_TITLE).then(resp => {
        this.fetchOptionResult(this.views[0].VOTE_ID).then(resp => {
          console.info('onRight', resp)
          if (resp.error === '0') {
            this.setOptionResult(resp.data.body)
            this.computePrecent()
          }
        })
      }).catch(error => {
        this.$store.dispatch('setTip', {text: error.message})
      })
    },
    postVote (VOTE_ID, OPTION_ID, OPTION_TITLE) {
      return checkLogin().then(userCode => {
        var params = {
          USER_CODE: userCode,
          VOTE_ID: VOTE_ID,
          RESULT_LIST: [{
            OPTION_ID: OPTION_ID,
            OPTION_TEXT: OPTION_TITLE
          }]
        }
        return RemoteDAO.callUC12008403(params)
      })
    },
    setOptionResult (results) {
      console.warn('setOptionResult', results)
      // this.views[0].OPTION_LIST = JSON.parse(this.views[0].OPTION_LIST)
      this.views[0].OPTION_LIST = results
      for (let i = 0; i < this.views[0].OPTION_LIST.length; i++) {
        var optionID = this.views[0].OPTION_LIST[i].OPTION_ID
        console.info('optionID', optionID)
        for (let j = 0; j < results.length; j++) {
          console.info('results[j].OPTION_ID', results[j].OPTION_ID)
          if (results[j].OPTION_ID === optionID) {
            this.$set(this.views[0].OPTION_LIST[i], 'OPTION_RESULT', parseInt(results[j].OPTION_RESULT))
            break
          }
        }
      }
      this.computePrecent()
    },
    onBack () {
      this.$router.go(-1)
    },
    fetchView () {
      var args = {
        VOTE_STATUS: '1',
        BUSIN_TYPE: 'NRNG',
        PAGE_SIZE: '10',
        PAGE_NO: '1'
      }
      RemoteDAO.callUC12008401(args).then(resp => {
        console.info('fetchView', resp)
        this.votes = resp.data.body || []
        // this.votes[0].IS_COMMENT = '0'
        this.isLoading = false
      }).catch(reject => {
        console.info('fetchView reject', reject)
        this.isLoading = false
      })
    },
    fetchOptionResult (VOTE_ID) {
      return RemoteDAO.callUC12008402({VOTE_ID: VOTE_ID})
    },
    onComment (id) {
      this.$router.push({name: 'Comments', query: {id: id}})
    },
    initUI () {
      window.onresize = () => {
        this.screenH = window.innerHeight
        this.screenW = window.innerWidth
      }
    }
  }
}
</script>

<style lang="less" module='s'>
.body{
  // width: 100%;
  padding-left: 5px;
  padding-right: 5px;
  padding-top: 15px;
  padding-bottom: 80px;
}
.wrap{
  background-image: url('assets/images/pkbg.jpg');
  background-size: cover;
  background-repeat: no-repeat;
  overflow: auto;
  .actionBar{
    box-shadow: 1px 0 1px 2px rgba(0,0,0,0.1);
    z-index: 2;
    background-color: #fff;
    .icon{
      width: 50%;
    }
  }
  .isLoading{
    margin-top: 20px;
    text-align: center;
    color: #999;
    font-size: 25px;
  }
  h1{
    font-size: 30px;
    text-align: center;
    padding: 10px;
  }
  .img{
    width: 85%;
    margin: auto;
    // background-color: #ccc;
    min-height: 300px;
    img{
      width: 100%;
    }
  }
  .contentWrap{
    // text-align: center;
    position: relative;
    .pre{
      margin: 0 auto;
      display: block;
      font-size: 23px;
      // visibility: hidden;
      min-height: 100px;
      width: 80%;
      white-space: pre-wrap;
      // line-height: px;
    }
  }
  
  .pk{
    background-image: url('assets/images/pkbg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    display: flex;
    margin-top: 20px;
    text-align: center;
    font-size: 28px;
    justify-content: center;
    padding: 25px 20px;
  }
  .left{
    flex: 1;
    color: red;
    // flex: 1;
    font-weight: bold;
    margin: 40px 0;
    background-image: url('../assets/images/hong.png');
    background-repeat: no-repeat;
    background-position: bottom;
    background-size: 110px;
  }
  .right{
    flex: 1;
    color: blue;
    // flex: 1;
    font-weight: bold;
    margin: 40px 0;
    background-image: url('../assets/images/lan.png');
    background-repeat: no-repeat;
    background-position: bottom;
    background-size: 110px;
  }
  .view{
    color: #000;
    font-size: 20px;
    font-weight: normal;
  }
  .mid{
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: flex-end;
    padding-bottom: 70px;
    margin: 0 10px;
  }
  .leftP .precent, .rightP .precent {
    min-height: 30px;
  }
  .leftP{
    text-align: left;
    .p{
      height: 10px;
      background-color: red;
      border-radius: 5px;
    }

  }
  .rightP{
    text-align: right;
    .p{
      height: 10px;
      background-color: blue;
      border-radius: 5px;
    }
  }
}
.history{
  font-size: 30px;
  font-weight: bold;
  padding-left: 20px;
  padding-right: 20px;
  padding-top: 20px;
  padding-bottom: 100px;
  .historyTitle{
    color: #000;
    font-weight: bold;
    font-size: 25px;
    margin-bottom: 5px;
  }
  .itemWrap{
    display: flex;
    align-items: center;
    // margin-left: 30px;
  }
  .point{
    width: 5px;
    height: 6px;
    border-radius: 50%;
    margin-right: 10px;
    background-color: #565656;
  }
  .view{
    font-size: 20px;
    padding: 5px 0;
    overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;
  }
}
.plIcon{
  width: 20%;
  margin: 10px;
  position: fixed;
  bottom: 100px;
  right: 20px;
}
.speak{
  box-shadow: -1px 0 1px 2px rgba(0,0,0,0.1);
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}
</style>
